package com.countryside.controller;

import com.countryside.pojo.CompanyWork;
import com.countryside.pojo.EmployeeWork;
import com.countryside.pojo.User;
import com.countryside.pojo.WorkCategory;
import com.countryside.pojo.query.CompanyWorkQuery;
import com.countryside.pojo.query.EmployeeWorkQuery;
import com.countryside.service.CompanyWorkService;
import com.countryside.service.EmployeeWorkService;
import com.countryside.service.UserService;
import com.countryside.service.WorkCategoryService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class IndexController {
    @Autowired
    CompanyWorkService companyWorkService;
    @Autowired
    EmployeeWorkService employeeWorkService;
    @Autowired
    WorkCategoryService workCategoryService;
    @Autowired
    UserService userService;

    /**
     * 进入主页
     * @param model
     * @param companyWorkQuery
     * @return
     */
    @RequestMapping({"/index", "/"})
    public String index(Model model, CompanyWorkQuery companyWorkQuery){
        companyWorkQuery.setPageSize(6);
        PageInfo<CompanyWork> companyWorkPageInfo = companyWorkService.listCompanyWork(companyWorkQuery);
        model.addAttribute("page", companyWorkPageInfo);
        List<WorkCategory> allWorkCategory = workCategoryService.getAllWorkCategory();
        model.addAttribute("allWorkCategory", allWorkCategory);
        return "index";
    }

    /**
     * 搜索
     * @param option
     * @param model
     * @param session
     * @param companyWorkQuery
     * @param employeeWorkQuery
     * @return
     */
    @PostMapping("/search")
    public String search(int option, Model model,
                         HttpSession session,
                         CompanyWorkQuery companyWorkQuery,
                         EmployeeWorkQuery employeeWorkQuery){
        if (option==0){
            //搜索招工信息
            companyWorkQuery.setTrade(companyWorkQuery.getAddress());
            companyWorkQuery.setTitle(companyWorkQuery.getAddress());
            PageInfo<CompanyWork> companyWorkPageInfo = companyWorkService.listCompanyWorkBySearch(companyWorkQuery);
            model.addAttribute("CWpage", companyWorkPageInfo);
        }else {
            //搜索工人信息
            employeeWorkQuery.setExpectedWork(employeeWorkQuery.getAddress());
            PageInfo<EmployeeWork> employeeWorkPageInfo = employeeWorkService.listEmployeeWorkBySearch(employeeWorkQuery);
            model.addAttribute("EWpage", employeeWorkPageInfo);
        }
        session.setAttribute("optionS", option);
        session.setAttribute("addressS", companyWorkQuery.getAddress());
        return "searchResult";
    }

    /**
     * 搜索
     * @param model
     * @param session
     * @param companyWorkQuery
     * @param employeeWorkQuery
     * @return
     */
    @GetMapping("/search")
    public String searchPage(Model model, HttpSession session,
                             CompanyWorkQuery companyWorkQuery,
                             EmployeeWorkQuery employeeWorkQuery){
        int option = (int) session.getAttribute("optionS");
        String address = (String) session.getAttribute("addressS");
        if (option==0){
            //搜索招工信息
            companyWorkQuery.setAddress(address);
            companyWorkQuery.setTrade(address);
            companyWorkQuery.setTitle(address);
            PageInfo<CompanyWork> companyWorkPageInfo = companyWorkService.listCompanyWorkBySearch(companyWorkQuery);
            model.addAttribute("CWpage", companyWorkPageInfo);
        }else {
            //搜索工人信息
            employeeWorkQuery.setAddress(address);
            employeeWorkQuery.setExpectedWork(address);
            PageInfo<EmployeeWork> employeeWorkPageInfo = employeeWorkService.listEmployeeWorkBySearch(employeeWorkQuery);
            model.addAttribute("EWpage", employeeWorkPageInfo);
        }
        return "searchResult";
    }

    @PostMapping("/updateWFirstUser")
    public String updateWUser(User user, HttpSession session) {
        User thisUser = (User) session.getAttribute("user");
        user.setId(thisUser.getId());
        user.setUserName(thisUser.getUserName());
        user.setPassword(thisUser.getPassword());
        user.setUserImg(thisUser.getUserImg());
        userService.updateUser(user);
        session.setAttribute("user", user);
        return "redirect:/findwork";
    }

    @PostMapping("/updateCFirstUser")
    public String updateCUser(User user, HttpSession session) {
        User thisUser = (User) session.getAttribute("user");
        user.setId(thisUser.getId());
        user.setUserName(thisUser.getUserName());
        user.setPassword(thisUser.getPassword());
        user.setUserImg(thisUser.getUserImg());
        userService.updateUser(user);
        session.setAttribute("user", user);
        return "redirect:/queryAllEmployeeWork";
    }



}
